package com.venus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.venus.entity.Consultant;
import com.venus.entity.Role;
import com.venus.entity.User;
import com.venus.entity.UserRole;
import com.venus.service.ConsultantService;
import com.venus.service.RoleService;
import com.venus.service.UserRoleService;
import com.venus.service.UserService;
import com.venus.util.PageUtil;
import com.venus.util.Result;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * user 用户表 前端控制器
 * </p>
 *
 * @author Venus
 * @since 2020-06-08
 */
@RestController
@RequestMapping("/user")
public class UserController {
    //初始化localDateTime
    LocalDateTime nowDataTime = LocalDateTime.now();
    LocalDateTime zeroDataTime = LocalDateTime.of(1111, 1, 1, 1, 1, 1);

    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    @Resource
    private UserRoleService userRoleService;
    @Resource
    private ConsultantService consultantService;


    @RequestMapping("/select")
    public Object selectUser(User user, PageUtil pu) {
        //获取分页信息
        //getPage()为当前页码  pu.getLimit()为每页显示条数
        Page<User> page = new Page<User>(pu.getPage(), pu.getLimit());
        //创建实体包装类   用于存放查询条件
        QueryWrapper<User> select = new QueryWrapper<User>();
        //根据名字模糊查询
        if (user.getUserName() != null && !user.getUserName().isEmpty()) {
            select.like("user_name", user.getUserName());
        }
        if (user.getUserPhone() != null && !user.getUserPhone().isEmpty()) {
            select.like("user_phone", user.getUserPhone());
        }
        if (user.getIsLockout() != null && !user.getIsLockout().isEmpty()) {
            select.eq("is_lockout", user.getIsLockout());
        }

        //分页查询方法
        page = userService.page(page, select);
        //getRecords()获取page中的集合数据
        //getTotal()获取总条数
        System.out.println(page.getTotal());
        System.out.println(page.getRecords());
        return new Result(0, "success", page.getRecords(), page.getTotal());
    }


    @RequestMapping("/create")
    public Object createUser(User user) {

        User userUp = userService.getOne(new QueryWrapper<User>().eq("user_name", user.getUserName()));
        if (userUp == null) {
            boolean create = userService.save(user);
            if (create == true) {
                return new Result(0, "添加成功");
            } else {
                return new Result(1, "添加失败！");
            }
        } else {
            return new Result(2, "名字重复！换一个名字吧！");
        }

    }


    @RequestMapping("/update")
    public Object updateUser(User user) {
        boolean update = userService.updateById(user);
        if (update == true) {
            return new Result(0, "修改陈功");
        } else {
            return new Result(1, "修改失败！");
        }
    }


    @RequestMapping("/delete")
    public Object deleteUser(Integer uid) {
        boolean del = userService.removeById(uid);
        if (del == true) {
            return new Result(0, "删除陈功");
        } else {
            return new Result(1, "删除失败！");
        }
    }

    @RequestMapping("/resetPwd")
    public Object updatePwd(User user) {
        user.setUserPassword("123456");
        boolean update = userService.updateById(user);
        if (update == true) {
            return new Result(0, "重置成功");
        } else {
            return new Result(1, "重置失败！");
        }
    }

    @RequestMapping("/lockUser")
    public Object lockUser(User user) {

        System.out.println("！！！！！！！！！！！！！！！！！！！！！！！！！！！");
        System.out.println("获取到的角色信息" + user.getUserId());
        System.out.println("获取到的状态信息" + user.getIsLockout());
        if (user.getIsLockout().equals("0")) {
            user.setIsLockout("1");
            user.setUserLockTime(nowDataTime);
            boolean update = userService.updateById(user);
            if (update == true) {
                return new Result(1, "已锁定！");
            } else {
                return new Result(2, "出问题了，刷新试试吧");
            }
        } else {
            user.setIsLockout("0");
            user.setUserLockTime(zeroDataTime);
            boolean update = userService.updateById(user);
            if (update == true) {
                return new Result(0, "已启用！");
            } else {
                return new Result(2, "出问题了，刷新试试吧");
            }
        }
    }

    @RequestMapping("/GetRolesAll")
    public Object getRolesAll() {
        List<Role> list = roleService.list();
        System.out.println("查询到的所有角色" + list);
        return list;
    }

    @RequestMapping("/getRolesByUid")
    public Object getRolesByUid(Integer uid) {
        List<Role> list = roleService.getRolesByUid(uid);
        System.out.println("查询到的该用户的所有角色" + list);
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("code", 0);
        map.put("msg", "");
        map.put("data", list);
        map.put("count", list.size());

        return map;

    }

    @RequestMapping("/addUserRole")
    public Object addUserToRole(UserRole userRole) {


        System.out.println(userRole.getUserId());
        System.out.println(userRole.getRoleId());
        boolean i = userRoleService.save(userRole);
        if (i == true) {
         //   Consultant con = new Consultant();
        //    con.setUserId(userRole.getUserId());
            //未完成
         //   con.setUserName("");
         //   boolean ii = consultantService.save(con);
         //   if (ii == true) {
                return new Result(0, "已删除");
         //   } else {
          //      return new Result(1, "出问题了，刷新试试吧");
         //   }
        } else {
            return new Result(1, "出问题了，刷新试试吧");
        }
    }

    @RequestMapping("/delUserRole")
    public Object removeUserFromRole(UserRole userRole) {
        QueryWrapper query = new QueryWrapper<UserRole>();
        query.eq("user_id", userRole.getUserId());
        query.eq("role_id", userRole.getRoleId());

        boolean i = userRoleService.remove(query);

        if (i == true) {
            boolean ii = consultantService.remove(new QueryWrapper<Consultant>().eq("user_id", userRole.getUserId()));
            if (ii == true) {
                return new Result(0, "已删除");
            } else {
                return new Result(1, "出问题了，刷新试试吧");
            }
        } else {
            return new Result(1, "出问题了，刷新试试吧");
        }
    }


}
